//
//  GeoCodeSearchManager.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/10.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "GeoCodeSearchManager.h"

@implementation GeoCodeSearchManager
{
    BMKGeoCodeSearchOption * _geocodeSearchOption; ///< 根据位置获取经纬度
    BMKReverseGeoCodeOption * _reverseGeocodeSearchOption; ///< 根据经纬度获取位置
    
    CLLocationCoordinate2D _location;
    NSString * _address;
}
/**
 *  根据位置获取经纬度
 */
- (void)geocodeSearchOptionWithAddress:(NSString *)address results:(GeocodeSearchOption)block
{
    _geocodeSearchOptionBlock = block;
    BMKGeoCodeSearchOption *geocodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geocodeSearchOption.city = @"北京市"; // 城市
    geocodeSearchOption.address = address; // 地址
    self.geocodesearch.delegate = self;
    BOOL flag = [self.geocodesearch geoCode:geocodeSearchOption];
    if(flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }
}

/**
 *  根据经纬度获取位置
 */
- (void)reverseGeocodeSearchOptionWithLocationCoordinate2D:(CLLocationCoordinate2D)pt results:(ReverseGeocodeSearchOption)block
{
    _ReverseGeocodeSearchOptionBlock = block;
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
    }

}
#pragma mark - 反向地理编码(经纬度转地址)
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == 0) {

        self.ReverseGeocodeSearchOptionBlock(result.address , result.addressDetail.city);
    }
}
#pragma mark - 正向地理编码(地址转经纬度)
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{

    if (error == 0) {

        self.geocodeSearchOptionBlock(result.location);
    }
}
- (instancetype)init
{
    self = [super init];
    if(self)
    {
        self.geocodesearch.delegate = self;
    }
    return self;
}

- (BMKGeoCodeSearch *)geocodesearch
{
    if(!_geocodesearch)
    {
        _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    }
    return _geocodesearch;
}

- (void)dealloc
{
    if(_geocodesearch)
    {
        _geocodesearch.delegate = nil;
        _geocodesearch = nil;
    }
}
@end
